package com.app.micaihu.view.user.dialog;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.collection.ArrayMap;

import com.app.base.BaseDialogFragment;
import com.app.http.NetObserver;
import com.app.http.RxLifecycleUtils;
import com.app.http.bean.BaseBean;
import com.app.micaihu.R;
import com.app.micaihu.ad.toutiao.TTAdManagerHolder;
import com.app.micaihu.configure.ApiService;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.ConfigKeyConstant;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.ApiServiceUtils;
import com.app.micaihu.utils.ParamsMapUtils;
import com.app.micaihu.view.bean.Arms;
import com.app.micaihu.view.bean.SignGold;
import com.app.micaihu.view.user.usertask.UserTaskActivity;
import com.app.utils.biz.Biz;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.blankj.utilcode.util.ViewUtils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import io.reactivex.rxjava3.functions.Consumer;

public class SignSuccessDialog extends BaseDialogFragment {

    public static SignSuccessDialog getInstance(Bundle bundle) {
        SignSuccessDialog signSuccessDialog = new SignSuccessDialog();
        signSuccessDialog.setArguments(bundle);
        return signSuccessDialog;
    }

    private TextView tvGold;
    private Button btnOk;
    private ImageView ivClose;
    private OnOpenClick onOpenClick;

    private String score;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_sign_success_ad;
    }

    @Override
    public void getBundle(@NotNull Bundle bundle) {
        super.getBundle(bundle);
        if (bundle != null) {
            score = bundle.getString(Config.Extra.gold);
        }
    }

    @Override
    protected void setData() {

        btnOk = getThisView().findViewById(R.id.btnOk);
        tvGold = getThisView().findViewById(R.id.tvGold);
        ivClose = getThisView().findViewById(R.id.ivClose);
        tvGold.setText("+" + score);
//        btnOk.setText("看视频再领" + extraRewardContent + "金币");
    }

    @Override
    protected void addListeners() {
        btnOk.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                SignSuccessDialog.this.dismiss();
//                MobclickAgentUtils.event(Config.EventId.signin_video, "签到激励视频");
                showVideoAd();
            }
        });
        ivClose.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                SignSuccessDialog.this.dismiss();
            }
        });
    }


    private void showVideoAd() {
        String mCodeId = SPUtils.getInstance().getString(ConfigKeyConstant.signAdId, "945838569");
        ;
        //模板广告需要设置期望个性化模板广告的大小,单位dp,代码位是否属于个性化模板广告，请在穿山甲平台查看
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(mCodeId)
                .setSupportDeepLink(true)
                .setRewardName("金币") //奖励的名称
                .setRewardAmount(3)  //奖励的数量
                //模板广告需要设置期望个性化模板广告的大小,单位dp,激励视频场景，只要设置的值大于0即可
                .setExpressViewAcceptedSize(ScreenUtils.getAppScreenWidth(), ScreenUtils.getAppScreenHeight())
                .setUserID(Biz.getInstance().uid())//用户id,必传参数
//                .setMediaExtra("media_extra") //附加参数，可选
                .setOrientation(TTAdConstant.VERTICAL) //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                .build();
        TTAdManagerHolder.get().createAdNative(Utils.getApp()).loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int code, String message) {
//                HashMap<String, String> map = AdMapUtils.getPositionMap();
//                map.put(AdMapUtils.adPositionRewardVideo + "onError", adSlot.getCodeId());
//                map.put(AdMapUtils.adPositionRewardVideo + "errorMsg", message);
//                map.put(AdMapUtils.adPositionRewardVideo + "errorCode", code + "");
//                MobclickAgentUtils.event(Config.EventId.toutiao_ad, map);
                ToastUtils.showShort("加载失败");
                LogUtils.e("id:" + adSlot.getCodeId() + " onError->" + code + " " + message);
            }

            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {
//                HashMap<String, String> map = AdMapUtils.getPositionMap();
//                map.put(AdMapUtils.adPositionRewardVideo + AdMapUtils.adSuccessKey, adSlot.getCodeId());
//                MobclickAgentUtils.event(Config.EventId.toutiao_ad, map);
                LogUtils.e("onRewardVideoAdLoad->" + ttRewardVideoAd);
                if (ttRewardVideoAd != null) {
                    Config.addGold = true;
                    ttRewardVideoAd.showRewardVideoAd(ActivityUtils.getTopActivity());
                    ttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
                        @Override
                        public void onAdShow() {
                            LogUtils.e("onRewardVideoAdLoad", "onAdShow()->");
                        }

                        @Override
                        public void onAdVideoBarClick() {
                            LogUtils.e("onRewardVideoAdLoad", "onAdVideoBarClick()->");
                        }

                        @Override
                        public void onAdClose() {
                            LogUtils.e("onRewardVideoAdLoad", "onAdClose()->");
                        }

                        @Override
                        public void onVideoComplete() {
                            LogUtils.e("onRewardVideoAdLoad", "onVideoComplete()->");
                        }

                        @Override
                        public void onVideoError() {
                            LogUtils.e("onRewardVideoAdLoad", "onVideoError()->");
                        }

                        @Override
                        public void onRewardVerify(boolean b, int i, String s, int i1, String s1) {
                            LogUtils.e("onRewardVideoAdLoad", "onRewardVerify()->" + b + " " + i + " " + s);
                        }

                        @Override
                        public void onSkippedVideo() {
                            LogUtils.e("onRewardVideoAdLoad", "onSkippedVideo()->");
                        }
                    });
                }
            }

            @Override
            public void onRewardVideoCached() {
//                HashMap<String, String> map = AdMapUtils.getPositionMap();
//                map.put(AdMapUtils.adPositionRewardVideo + AdMapUtils.adRewardVideoCacheKey, adSlot.getCodeId());
//                MobclickAgentUtils.event(Config.EventId.toutiao_ad, map);
                LogUtils.e("onRewardVideoAdLoad", "onRewardVideoCached->");
            }
        });
    }



//    @Override
//    protected boolean getCanceledOnTouchOutside() {
//        return true;
//    }

    public interface OnOpenClick {

        void onAddGold(int gold);
    }

    public void setOnOpenClick(OnOpenClick onOpenClick) {
        this.onOpenClick = onOpenClick;
    }


}
